<?php
namespace App\DBs\Admin;

use Illuminate\Support\Facades\DB;
use App\DBs\BaseDB as Base;

class FrontDB extends Base
{
    //前台菜单

    public function __construct()
    {
    }

    //列表
    public function getIndex()
    {
        $items = [
            ['mtype'=>1,'title'=>'顶部banner菜单','childs'=>[]],
        ];
        foreach ($items as $k => $v) {
            $res = DB::table($this->tFront)->where('del',0)->where('mtype',$v['mtype'])
                ->orderBy('id','asc')->get();
            if (count($res)) {
                $res = $this->jsonToArr($res);
                foreach ($res as $re) {
                    $items[$k]['childs'][] = [
                        'id'=>$re['id'], 'title'=>$re['title'], 'link'=>$re['link'],
                        'cn'=>$re['cn'], 'en'=>$re['en'],
                    ];
                }
            }
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //设置前台菜单
    public function setUpdate($id,$arys)
    {
        $arys['uptime'] = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tFront)->where('del',0)->where('id',$id)->first();
        $resUp = DB::table($this->tFront)->where('id',$id)->update($arys);
        if (!$resUp) {
            return array('code'=>-2,'msg'=>'出错辣');
        }
        return array('code'=>0,'msg'=>'success');
    }

    //页面语言
    public function getLangIndex()
    {
        //1首页，2大会动态，3大会相关，4学术委员会，5关于我们
        $items = [
            ['id'=>1,'title'=>'首页','childs'=>[]],
            ['id'=>2,'title'=>'大会动态','childs'=>[]],
            ['id'=>3,'title'=>'大会相关','childs'=>[]],
            ['id'=>4,'title'=>'关于我们','childs'=>[]],
        ];
        foreach ($items as $k => $v) {
            $res = DB::table($this->tLang)->where('del',0)->where('vtype',$v['id'])
                ->orderBy('id','asc')->get();
            if (count($res)) {
                $res = $this->jsonToArr($res);
                foreach ($res as $re) {
                    $items[$k]['childs'][] = [
                        'id'=>$re['id'], 'istitle'=>$re['istitle'],
                        'cn'=>$re['cn'], 'en'=>$re['en'], 'cssid'=>$re['cssid'],
                    ];
                }
            }
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //首页横图
    public function getAdIndex()
    {
        $res = DB::table($this->tImgs)->where('del',0)->where('type',1)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = env('DOMAIN').'/'.$re['img'];
            $items[] = [
                'id'=>$re['id'], 'img'=>$img,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //首页横图新增
    public function setAdAdd($img)
    {
        $totime = date('Y-m-d H:i:s',time());
        $upData = ['type'=>1,'img'=>$img,'addtime'=>$totime];
        $res = DB::table($this->tImgs)->insertGetId($upData);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0,'msg'=>'success');
    }

    //首页横图删除
    public function setAdDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tImgs)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0,'msg'=>'success');
    }

    //赞助商
    public function getSponsorIndex()
    {
        $res = DB::table($this->tImgs)->where('del',0)->where('type',2)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = env('DOMAIN').'/'.$re['img'];
            $items[] = [
                'id'=>$re['id'], 'img'=>$img,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //赞助商新增
    public function setSponsorAdd($img)
    {
        $totime = date('Y-m-d H:i:s',time());
        $upData = ['type'=>2,'img'=>$img,'addtime'=>$totime];
        $res = DB::table($this->tImgs)->insertGetId($upData);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0,'msg'=>'success');
    }

    //赞助商删除
    public function setSponsorDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tImgs)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0,'msg'=>'success');
    }
}